package handler

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"github.com/gocolly/colly"
	"log"
	"strings"
)

func viewCompanyHandler(code string) {
	url := fmt.Sprintf("https://www.icama.cn/BasicdataSystem/pesticideRegistration/viewcompany.do?certificateCode=%s", code)

	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))

	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "JSESSIONID=D4F6BAB7AE0FC35310CF83EB3ECDF014; pageSize=30; pageNo=1")

	})
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			log.Fatal(err)
		}
		nodes := htmlquery.Find(doc, `//*[@id="tab"]/tbody/tr`)
		for key, node := range nodes {
			if key == 0 {
				continue
			}
			a := htmlquery.FindOne(node, "./td[1]/span/a")
			fmt.Println(htmlquery.InnerText(a), htmlquery.SelectAttr(a, "onclick"))
		}
	})
	c.Visit(url)
}
